package controllers.admin.Payment;

import controllers.admin.Logo;
import models.Models.Email;
import models.Models.ShopRechargeViewModels;
import models.System.Headline.Payment;
import models.SystemModels.ShopRecharge;
import models.SystemModels.SystemUser;
import models.SystemModels.SystemUserDetails;
import models.SystemModels.SystemUserEmails;
import models.interfaces.Systems;
import net.sf.json.JSONObject;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-08-04.
 */
public class payment extends Logo {

   static Systems systems =new  models.System.Systems.Systems();
   static  Systems db = new models.System.Systems.Systems();
    public static void index(){
        List<SystemUserDetails> list =  SystemUserDetails.findAll();
        List<Payment> payments = Payment.payments();
        List<Payment> paymentList = Payment.reducePaymsg();
        List<SystemUser> systemUsersList = SystemUser.findAll();
        render(list,payments,systemUsersList,paymentList);
    }

    public static void paymsg(){
        JSONObject json = new JSONObject();
        //获取参数
        Long payments_Id =  Long.parseLong(params.get("Payment"));
        Long systemUsersList_Id = Long.parseLong(params.get("systemUsersList"));
        String type = params.get("type");
        String payment_title = null;
        //获取标题
        double moneys = Double.parseDouble(params.get("moneys"));
        for (Payment item:Payment.payments()) {
            if (item.Id == payments_Id){
                payment_title = item.title;
            }
        }
        //保存消费信息
        List<SystemUserDetails> system = SystemUserDetails.find("UserId",systemUsersList_Id).fetch();
        try{
            ShopRecharge shopre = new ShopRecharge();
            shopre.time= db.date(new Date());
            shopre.price = 0.00;
            shopre.Total_balance =system.get(0).money+moneys;
            shopre.balance = 0.00;
            shopre.type = payment_title;
            shopre.Odd_Numbers = systems.ids("ODD");
            shopre.user= systemUsersList_Id.toString();
            shopre.save();
        }catch (Exception ex){
            json.put("code",-1);
            renderJSON(json);
        }
        //更新用户信息
        SystemUserDetails systemUser = SystemUserDetails.findById(system.get(0).Id);
        try{
            systemUser.money= system.get(0).money+moneys;
            systemUser.save();
        }catch (Exception ex){
            json.put("code",-2);
            renderJSON(json);
        }
       //更新用户消息记录
        String msg=String.format("亲爱的用户，%s送您%s个图币，余额为%s",payment_title,moneys,system.get(0).money+moneys);
        SystemUserEmails systemEmails = new SystemUserEmails();

        try{
            systemEmails.Date = db.date(new Date());
            systemEmails.UserId =systemUsersList_Id;
            systemEmails.msg=msg;
            systemEmails.title =payment_title;
            systemEmails.type = 0;
            systemEmails.save();
        }catch (Exception ex){
            json.put("code",-3);
            renderJSON(json);
        }
        //发送邮件
        try{
            if (type.equals("1")){
                Email email = new Email();
                email.account = system.get(0).Email;
                email.msg = msg;
                email.title = payment_title;
                models.send.email.MsgEmail(email);
            }
        }catch (Exception ex){
            json.put("code",-4);
            renderJSON(json);
        }
        json.put("code",1);
        renderJSON(json);
    }

    public static void reducePaymsg(){
        JSONObject json = new JSONObject();
        //获取参数
        Long payments_Id =  Long.parseLong(params.get("Payment"));
        Long systemUsersList_Id = Long.parseLong(params.get("systemUsersList"));
        String type = params.get("type");
        String payment_title = null;
        //获取标题
        double moneys = Double.parseDouble(params.get("moneys"));
        for (Payment item:Payment.payments()) {
            if (item.Id == payments_Id){
                payment_title = item.title;
            }
        }
        //保存消费信息
        List<SystemUserDetails> system = SystemUserDetails.find("UserId",systemUsersList_Id).fetch();
        if((system.get(0).money-moneys)<0){
            json.put("code",0);
            renderJSON(json);
        }else{
            try{
                ShopRecharge shopre = new ShopRecharge();
                shopre.time= db.date(new Date());
                shopre.price = 0.00;
                shopre.Total_balance =system.get(0).money-moneys;
                shopre.balance = 0.00;
                shopre.type = payment_title;
                shopre.Odd_Numbers = systems.ids("ODD");
                shopre.user= systemUsersList_Id.toString();
                shopre.save();
            }catch (Exception ex){
                json.put("code",-1);
                renderJSON(json);
            }
            //更新用户信息
            SystemUserDetails systemUser = SystemUserDetails.findById(system.get(0).Id);
            try{
                systemUser.money= system.get(0).money-moneys;
                systemUser.save();
            }catch (Exception ex){
                json.put("code",-2);
                renderJSON(json);
            }
            //更新用户消息记录
            String msg=String.format("亲爱的用户，%s您%s个图币，余额为%s",payment_title,moneys,system.get(0).money+moneys);
            SystemUserEmails systemEmails = new SystemUserEmails();
            try{
                systemEmails.Date = db.date(new Date());
                systemEmails.UserId =systemUsersList_Id;
                systemEmails.msg=msg;
                systemEmails.title =payment_title;
                systemEmails.type = 0;
                systemEmails.save();
            }catch (Exception ex){
                json.put("code",-3);
                renderJSON(json);
            }
            //发送邮件
            try{
                if (type.equals("1")){
                    Email email = new Email();
                    email.account = system.get(0).Email;
                    email.msg = msg;
                    email.title = payment_title;
                    models.send.email.MsgEmail(email);
                }
            }catch (Exception ex){
                json.put("code",-4);
                renderJSON(json);
            }
        }

        json.put("code",1);
        renderJSON(json);
    }

    public static void payment_Index(){
        List<ShopRechargeViewModels> list1 =ShopRechargeViewModels.ShopRechargeViewModelsList();
        render(list1);
    }
}
